package com.bb2ca;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import com.bb2ca.R.id;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class BB2CAActivity extends Activity {
	
	/*
	 * initialization
	 */
	private static final String TAG = "BB2CA";
	private static final int MAX_DATA = 50;
	private String[][] listBBS = new String[MAX_DATA][MAX_DATA];
	private String[][] urlBBS = new String[MAX_DATA][MAX_DATA];
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        readBBS(listBBS, urlBBS);
        
        /*
         * listView set
         */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        for (int i = 0; i < MAX_DATA - 1; i++) {
        	if (listBBS[i][0] != null) {
        		adapter.add(listBBS[i][0]);
        	}
        }
        ListView listView = (ListView) findViewById(id.listBBS);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListView listView = (ListView) parent;
                long item = listView.getItemIdAtPosition(position);
                Log.d(TAG,""+item);
                Toast.makeText(BB2CAActivity.this, ""+item, Toast.LENGTH_LONG).show();
            }
        });
        // ���X�g�r���[�̃A�C�e�����I�����ꂽ���ɌĂяo�����R�[���o�b�N���X�i�[��o�^���܂�
/*        listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                ListView listView = (ListView) parent;
                // �I�����ꂽ�A�C�e�����擾���܂�
                String item = (String) listView.getSelectedItem();
                Toast.makeText(BB2CAActivity.this, item, Toast.LENGTH_LONG).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
*/      
        
    }
    

    /*
     * readBBS(String[] listBBS, String[][] listThread)
     * set the names of BBS to listBBS
     * set each URLs to urlBBS
     */
    private void readBBS(String[][] listBBS, String[][] urlBBS) {
		listBBS[0][0] = "最近見た掲示板";
		listBBS[1][0] = "よく見る掲示板";
		int i = 2, j = 0;
		
		getBBSMenu();

		try{
			InputStream in = openFileInput("bbsmenu.html");
			BufferedReader br = new BufferedReader(new InputStreamReader(in,"SJIS"));
			
			String line = "";
			String line2 = "";
		      while ((line = br.readLine()) != null) {
		    	  if (line.matches("<BR><BR><B>.*</B><BR>")){
		    		  listBBS[i][0] = line.substring(11,line.length()-8);
		    		  
		    		  // set each URLs to urlBBS
		    		  while ((line2 = br.readLine()).length() != 0) {
		    			  urlBBS[i][j] = line2;
		    			  j++;
		    		  }
		    		  i++; j = 0;
		    	  }
		      }
		      br.close();
		    } catch (FileNotFoundException e) {
		      Log.e(TAG,""+e);
		    } catch (IOException e) {
			      Log.e(TAG,""+e);
		} 
	}


	/*
	 * getBBSMenu
	 * save "bbsmenu.html" to local file
	 */
	private void getBBSMenu() {
		HttpURLConnection http = null;
		InputStream in = null;
		try {
		      URL url = new URL("http://menu.2ch.net/bbsmenu.html");
		      http = (HttpURLConnection)url.openConnection();
		      http.setRequestMethod("GET");
		      http.connect();
		      in = http.getInputStream();

		      OutputStream out = openFileOutput("bbsmenu.html",MODE_PRIVATE);
		      PrintWriter writer = new PrintWriter(new OutputStreamWriter(out,"SJIS"));

		      BufferedReader br = new BufferedReader(new InputStreamReader(in,"SJIS"));
		      String line = "";
		      while ((line = br.readLine()) != null) { writer.append(new String(line)+"\n"); }
		      writer.close();
		} catch (Exception e) {
			Log.e(TAG,""+e);
	    } finally {
		    	try {
		    		if(http != null)
		    			http.disconnect();
		    		if(in != null) 
		    			in.close();
		    	} catch (Exception e) {}
		    }
	}
	
	/*
	 * 
	 */
	
}